﻿<%
' MLHostClass:
'	load()
'	reload()
'	getBlogs()
'	getTopNewItems
'	getTopReferdItems

class MLHostClass
    public property get CACHE_PREFIX 
        CACHE_PREFIX = "MLHOST"
    end property
    
    private m_blogObjs, m_topNewItemIDs, m_topReferedItemIDs, m_topReferedNewItemIDs

    private sub class_initialize()

    end sub
    
    private sub class_terminate()
    
    end sub
    
    private function build(array)
        build = false
        if not isArray(array) then
            exit function
        end if
        if ubound(array)<4 then
            exit function
        end if
        if not IsArray(array(0)) then
            exit function
        end if

        dim count, i
        count = ubound(array(0))
        redim m_blogObjs(count)

        if count<1 then
            exit function
        end if
        
        for i=0 to count-1 
            set m_blogObjs(i) = new BlogClass
            m_blogObjs(i).build(array(0)(i))
        next
        
        m_topNewItemIDs = array(1)
        m_topReferedItemIDs = array(2)
        m_topReferedNewItemIDs = array(3)
        
        build = true
    end function        
    

    private function toArray()
    
        if isEmpty(m_blogObjs) or not isArray(m_blogObjs) then
            exit function
        end if
            
        dim array(4), blogArray,i, count
        count = ubound(m_blogObjs)
        redim blogArray(count)
        
        if count>0 then
            for i=0 to count-1 
                blogArray(i) = m_blogObjs(i).toArray()
            next        
        end if
        
        array(0) = blogArray
        array(1) = m_topNewItemIDs
        array(2) = m_topReferedItemIDs
        array(3) = m_topReferedNewItemIDs
        
        toArray = array
    end function
    

    public function load()
        load = false

        load = loadFromCache
        if load=false then
            load = loadFromDB()
            if (load=true) then
                call cache()
            end if    
        end if    
    end function    

    public function reload()
        reload = false

        reload = loadFromDB()
        if reload=true then
            call cache()
        else
            call clean()
        end if    
    end function
    
    public function clean()
        call removeFromCache(CACHE_PREFIX, MISSLOG_HOST_ID)
        m_blogObjs = Empty
        m_topNewItemIDs = Empty
        m_topReferedItemIDs = Empty
    end function        

    private function loadFromCache()
        loadFromCache = build(getFromCache(CACHE_PREFIX, MISSLOG_HOST_ID))
    end function
        
    public sub cache()
       call build(putIntoCache(CACHE_PREFIX, MISSLOG_HOST_ID, toArray()))
    end sub

    private function loadFromDB()
		loadFromDB = true
	end function    

    private function loadBlogsFromDB()
        call openDB
        
        dim sql, rs
        set rs = Server.CreateObject("ADODB.Recordset")
                
        sql = " select blog_id, blog_title, blog_desc,banner_Url, photo_Url, page_Size, show_Content, user_id, user_name from blog order by blog_id " 
        rs.open sql, conn, 1, 1
        if rs.eof then
            loadFromDB = false
            exit function
        end if
        
        dim  count, i
        count = rs.recordCount
        redim m_blogObjs(count)
        for i=0 to count-1
            set m_blogObjs(i) = new BlogClass
            call m_blogObjs(i).buildFromRS(rs)
            rs.moveNext
        next
        
        rs.close
        set rs = nothing

        loadBlogsFromDB = true
    end function
    
    public function getBlogs()
        if isEmpty(m_blogObjs) then
            call loadBlogsFromDB()
            call cache()
        end if    
        getBlogs = m_blogObjs
    end function
        
    public function getNearsetBlogs(blog_id)
    	dim nearestBlogs, allBlogs, count, pageSize
    	pageSize = 20
    	if isEmpty(blog_id) or clng(blog_id)<1 then
    		redim nearestBlogs(0)
        else
    		allBlogs = getBlogs()
    		count = ubound(allBlogs)
    		if count<=pageSize then
    			nearestBlogs = allBlogs
    		else
    			redim nearestBlogs(pageSize)
    			
    			dim pos, i, pstart
    			
    			pos = clng(blog_id) -1 
    			
				do while pos >= 0 and clng(allBlogs(pos).blogId) <> clng(blog_id)
					pos = pos - 1
				loop

				pstart = pos - pageSize\2
				if pstart<0 then
					pstart = 0
				end if
				
				if pstart + pageSize > count then
					pstart = count - pageSize
				end if
				
			    for i=0 to pageSize-1
			        set nearestBlogs(i) = allBlogs(pstart + i)
			    next					
    		end if
    	end if 
    	
    	getNearsetBlogs = nearestBlogs
    	
    end function
      
          
    public function loadTopNewItemsFromDB()
        dim sql, rs
        set rs = Server.CreateObject("ADODB.Recordset")

        sql = " select top 20 a.content_id from blog_content a , blog_user_group c  where a.blog_id=c.blog_id and c.group_id=4 and c.can_read=true and a.is_secret=false  order by a.insert_time desc"
        
        call openDB
        
        rs.open sql,conn,1,1

        dim count,i
        count = rs.recordCount
        redim m_topNewItemIDs(count)
        'response.write count
        if count>0 then
            for i=0 to count -1        
                m_topNewItemIDs(i) = rs(0)
                rs.moveNext()
            next
        end if    
        rs.close
        set rs = nothing
    end function
    
    public function getTopNewItems
        if isEmpty(m_topNewItemIDs) then
            call loadTopNewItemsFromDB()
            call cache()
        end if    
        
        dim count
        count = ubound(m_topNewItemIDs)
        if count<1 then
            exit function
        end if 
        
        dim blogItem, i
        redim blogItems(count)
        for i=0 to count-1 
            set blogItem = new BlogItemClass
            call blogItem.load(m_topNewItemIDs(i))
            set blogItems(i) = blogItem
        next
        
        getTopNewItems = blogItems
    end function
        
    private function loadTopReferedItemsFromDB()
        dim sql, rs
        set rs = Server.CreateObject("ADODB.Recordset")

        sql = "select top 20 * from (select b.content_id , count(*) as cc from blog_content b,blog_referer c where b.content_id=c.content_id and c.content_id is not null  group by b.content_id) order by cc desc,content_id desc"
        
        call openDB
        
        rs.open sql,conn,1,1

        dim count,i
        count = rs.recordCount
        redim m_topReferedItemIDs(count)
        'response.write count
        if count>0 then
            for i=0 to count -1        
                m_topReferedItemIDs(i) = rs(0)
                rs.moveNext()
            next
        end if    
        rs.close
        set rs = nothing
    end function
    
    public function getTopReferedItems
        if isEmpty(m_topReferedItemIDs) then
            call loadTopReferedItemsFromDB()
            call cache()
        end if    
        
        dim count
        count = ubound(m_topReferedItemIDs)
        if count<1 then
            exit function
        end if 
        
        dim blogItem, i
        redim blogItems(count)
        for i=0 to count-1 
            set blogItem = new BlogItemClass
            call blogItem.load(m_topReferedItemIDs(i))
            set blogItems(i) = blogItem
        next
        
        getTopReferedItems = blogItems
    end function    
        
        
        
    private function loadTopReferedNewItemsFromDB()
        dim sql, rs
        set rs = Server.CreateObject("ADODB.Recordset")

        'sql = "select top 20 * from (select b.content_id , count(*) as cc from blog_content b,blog_referer c where b.content_id=c.content_id and c.content_id is not null  and b.insert_time>=DATEADD('m',-3,DATE()) and c.insert_time>=DATEADD('m',-1,DATE()) group by b.content_id) order by cc desc"
        sql = "select top 20 * from (select b.content_id , count(*) as cc from blog_content b,blog_referer c where b.content_id=c.content_id and c.content_id is not null   and c.insert_time>=DATEADD('m',-1,DATE()) group by b.content_id) order by cc desc,content_id desc"
        call openDB

        rs.open sql,conn,1,1

        dim count,i
        count = rs.recordCount
        redim m_topReferedNewItemIDs(count)
        'response.write count
        if count>0 then
            for i=0 to count -1        
                m_topReferedNewItemIDs(i) = rs(0)
                rs.moveNext()
            next
        end if    
        rs.close
        set rs = nothing
    end function
    
    public function getTopReferedNewItems
        if isEmpty(m_topReferedNewItemIDs) then
            call loadTopReferedNewItemsFromDB()
            call cache()
        end if    
        
        dim count
        count = ubound(m_topReferedNewItemIDs)
        if count<1 then
            exit function
        end if 
        
        dim blogItem, i
        redim blogItems(count)
        for i=0 to count-1 
            set blogItem = new BlogItemClass
            call blogItem.load(m_topReferedNewItemIDs(i))
            set blogItems(i) = blogItem
        next
        
        getTopReferedNewItems = blogItems
    end function    
                
end class
%>